IntParser   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 34
dl 0
loc 109
rs 10
c 0
b 0
f 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A constructor 0 31 2
A unpackSigned_ 0 3 1
A clamp_ 0 8 3
A sign_ 0 6 2
A unpack_ 0 8 2
A pack 0 8 2
1
/*
2
 * Copyright (c) 2017-2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview Encode and decode int numbers to and from byte buffers.
27
 * @see https://github.com/rochars/byte-data
28
 * @see https://github.com/rochars/wavefile
29
 */
30
31
/**
32
 * A class to write and read integer numbers to and from byte buffers.
33
 */
34
export class IntParser {
35
  
36
  /**
37
   * @param {number} bits The number of bits used by the integer.
38
   * @param {boolean} [signed=false] True for signed, false otherwise.
39
   */
40
  constructor(bits, signed=false) {
41
    /**
42
     * The number of bits used by one number.
43
     * @type {number}
44
     */
45
    this.bits = bits;
46
    /**
47
     * The number of bytes used by one number.
48
     * @type {number}
49
     */
50
    this.offset = Math.ceil(bits / 8);
51
    /**
52
     * @type {number}
53
     * @protected
54
     */
55
    this.max = Math.pow(2, bits) - 1;
56
    /**
57
     * @type {number}
58
     * @protected
59
     */
60
    this.min = 0;
61
    /**
62
     * @type {Function}
63
     */
64
    this.unpack = this.unpack_;
65
    if (signed) {
66
      this.max = Math.pow(2, bits) / 2 - 1;
67
      this.min = -this.max - 1;
68
      this.unpack = this.unpackSigned_;
69
    }
70
  }
71
72
  /**
73
   * Write one unsigned integer to a byte buffer.
74
   * @param {!(Uint8Array|Array<number>)} buffer An array of bytes.
75
   * @param {number} num The number. Overflows are truncated.
76
   * @param {number} [index=0] The index being written in the byte buffer.
77
   * @return {number} The next index to write on the byte buffer.
78
   */
79
  pack(buffer, num, index=0) {
80
    num = this.clamp_(Math.round(num));
81
    for (let i = 0, len = this.offset; i < len; i++) {
82
      buffer[index] = Math.floor(num / Math.pow(2, i * 8)) & 255;
83
      index++;
84
    }
85
    return index;
86
  }
87
88
  /**
89
   * Read one unsigned integer from a byte buffer.
90
   * Does not check for overflows.
91
   * @param {!(Uint8Array|Array<number>)} buffer An array of bytes.
92
   * @param {number} [index=0] The index to read.
93
   * @return {number}
94
   * @private
95
   */
96
  unpack_(buffer, index=0) {
97
    /** @type {number} */
98
    let num = 0;
99
    for(let x = 0; x < this.offset; x++) {
100
      num += buffer[index + x] * Math.pow(256, x);
101
    }
102
    return num;
103
  }
104
105
  /**
106
   * Read one two's complement signed integer from a byte buffer.
107
   * @param {!(Uint8Array|Array<number>)} buffer An array of bytes.
108
   * @param {number} [index=0] The index to read.
109
   * @return {number}
110
   * @private
111
   */
112
  unpackSigned_(buffer, index=0) {
113
    return this.sign_(this.unpack_(buffer, index));
114
  }
115
116
  /**
117
   * Clamp values on overflow.
118
   * @param {number} num The number.
119
   * @private
120
   */
121
  clamp_(num) {
122
    if (num > this.max) {
123
      return this.max;
124
    } else if (num < this.min) {
125
      return this.min;
126
    }
127
    return num;
128
  }
129
130
  /**
131
   * Sign a number.
132
   * @param {number} num The number.
133
   * @return {number}
134
   * @private
135
   */
136
  sign_(num) {
137
    if (num > this.max) {
138
      num -= (this.max * 2) + 2;
139
    }
140
    return num;
141
  }
142
}
143